﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class SplitPipe : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			this.CommandData = commandData;
			this.UiApp = commandData.Application;
			this.UiDoc = this.UiApp.ActiveUIDocument;
			this.doc = this.UiDoc.Document;
			Selection selection = this.UiDoc.Selection;
			List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
			if (!YJKRevitTools.ViewCheck(this.UiDoc, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			FrmSplitPipe frmSplitPipe = new FrmSplitPipe(this.doc);
			Element ele = null;
			Transaction tran = new Transaction(this.doc, "管线分段");
			try
			{
				frmSplitPipe.ShowDialog();
				if (frmSplitPipe.IsPickup)
				{
					while (frmSplitPipe.IsPickup)
					{
						ele = this.GetSelectionElement(selection);
						frmSplitPipe = new FrmSplitPipe(this.doc, ele);
						frmSplitPipe.ShowDialog();
					}
					if (frmSplitPipe.DialogResult == DialogResult.OK)
					{
						if (frmSplitPipe.cbNumber.Checked && this.doc.ActiveView is View3D && !((View3D)this.doc.ActiveView).IsLocked)
						{
							YJKMessageBox.Information("在三维视图中不锁定视图无法标注管线编号!");
							return Autodesk.Revit.UI.Result.Succeeded;
						}
						new SplitPipeOperation(this.doc, this.UiDoc, this.UiApp, this.CommandData).SplitLinePipe(ele, frmSplitPipe, tran);
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				return Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.ToString());
				if (!frmSplitPipe.IsDisposed)
				{
					frmSplitPipe.Close();
					frmSplitPipe.Dispose();
				}
			}
			finally
			{
				this.EndTransation(tran, true);
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private void EndTransation(Transaction tran, bool beSure)
		{
			if (tran != null)
			{
				TransactionStatus status = tran.GetStatus();
				if ((int)status == 1)
				{
					if (beSure)
					{
						tran.Commit();
						return;
					}
					tran.RollBack();
					return;
				}
				else if ((int)status == 5)
				{
					tran.RollBack();
				}
			}
		}

		private Element GetSelectionElement(Selection sel)
		{
			LinePipeFilter linePipeFilter = new LinePipeFilter();
			Reference reference = sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, linePipeFilter, "选择管线！");
			return this.doc.GetElement(reference);
		}

		private Document doc;

		private UIDocument UiDoc;

		private List<MEPCurve> linesForNum = new List<MEPCurve>();

		private UIApplication UiApp;

		private ExternalCommandData CommandData;
	}
}
